package com.xbongbong.paas.service.workflow.setting.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.StageProcessEntity;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.model.StageProcessModel;
import com.xbongbong.paas.pojo.workflow.settings.WorkflowGetPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.paas.util.WorkflowNodeUtil;
import com.xbongbong.pro.enums.workflow.WorkflowValueTypeEnum;
import com.xbongbong.workflow.pojo.nodes.WorkflowConditionPojo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description 流转条件保存阶段推进器字段处理
 * @Author sunshaoxiang
 * @Date 2021/12/8 11:25
 * @Version 1.0
 */
@Service("ConditionNodeStageServiceImpl")
public class ConditionNodeStageServiceImpl extends AbstractConditionNodeFieldService {

    @Resource
    private ProFormHelp proFormHelp;
    @Resource
    private StageProcessModel stageProcessModel;

    @Override
    public List<Integer> type() {
        return Arrays.asList(FieldTypeEnum.STAGE_THRUSTER.getType());
    }

    @Override
    public String conditionField(WorkflowConditionPojo workflowConditionPojo, WorkflowGetPojo workflowGetPojo) throws XbbException {
        String value = "";
        Integer businessType = workflowConditionPojo.getBusinessType();
        Long formId = workflowConditionPojo.getFormId();
        Integer saasMark = workflowConditionPojo.getSaasMark();
        String corpid = workflowGetPojo.getCorpid();
        Map<String, FieldAttrEntity> explainMap = WorkflowNodeUtil.getExplainMap(workflowGetPojo, workflowConditionPojo.getBusinessType(), workflowConditionPojo.getFormId());
        FieldAttrEntity fieldAttrEntity = explainMap.get(workflowConditionPojo.getAttr());
        //阶段推进器字段的流转条件是当前启用版本的阶段
        List<ItemPoJo> itemList = new ArrayList<>();
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("formId", formId);
        param.put("del", 0);
        List<StageProcessEntity> stageProcessList = stageProcessModel.findEntitys(param);
        for (StageProcessEntity stageProcess : stageProcessList) {
            List<ItemPoJo> itemPojos = proFormHelp.getItemPojoForStageEntity(corpid, formId, stageProcess.getId(), businessType, saasMark, null);
            itemList.addAll(itemPojos);
        }
        // List<ItemPoJo> itemList = proFormHelp.getEnableStageComboItems(saasMark, businessType, corpid, formId);
        fieldAttrEntity.setItems(itemList);
        String attrName = fieldAttrEntity.getAttrName();
        String symbol = ConditionEnum.getMemoBySymbo(workflowConditionPojo.getSymbol());
        if (Objects.equals(WorkflowValueTypeEnum.FIXED_VALUE.getType(), workflowConditionPojo.getValueType())) {

            if (CollectionUtils.isNotEmpty(workflowConditionPojo.getValue())) {
                List<Object> valueList = new ArrayList<>();
                workflowConditionPojo.getValue().forEach(item->{
                JSONObject jsonObject = new JSONObject();
                jsonObject.put(workflowConditionPojo.getAttr(), item);
                valueList.add(ItemUtil.getCustomItemList(fieldAttrEntity, jsonObject));
                });
                value = attrName + symbol + StringUtils.join(valueList, "、");
            } else {
                // 为空、不为空
                value = attrName + symbol;
            }
        } else {
            value = getDynamicValue(attrName, symbol, workflowGetPojo, workflowConditionPojo);
        }
        return value;
    }

}
